/*
 * @Author: wujian
 * @Date: 2023-04-06 11:26:32
 * @LastEditors: wujian
 * @LastEditTime: 2023-04-06 15:04:27
 * @FilePath: \sys\vite.config.ts
 * @Description:
 * @email: 281919544@qq.com
 * Copyright (c) 2023 by wujian, All Rights Reserved.
 */
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import { defineConfig, loadEnv, ConfigEnv } from 'vite';

import topLevelAwait from 'vite-plugin-top-level-await';

import qiankun from 'vite-plugin-qiankun-deer';

const pathResolve = (dir: string): any => {
	return resolve(__dirname, '.', dir);
};

const alias: Record<string, string> = {
	'/@': pathResolve('./src/'),
	'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
};

const viteConfig = defineConfig((mode: ConfigEnv) => {
	const env = loadEnv(mode.mode, process.cwd());
	return {
		plugins: [
			vue(),
			topLevelAwait({
				// The export name of top-level await promise for each chunk module
				promiseExportName: '__tla',
				// The function to generate import names of top-level await promise in each chunk module
				promiseImportName: (i) => `__tla_${i}`,
			}),
			qiankun('app-sms', { useDevMode: true }),
		],
		root: process.cwd(),
		resolve: { alias },
		base: mode.mode === 'development' ? '/' : '/subapp/sms/',
		// base: mode.mode === 'development' ? '/' : '/subapp/sys/',
		hmr: true,
		optimizeDeps: {
			include: ['element-plus/lib/locale/lang/zh-cn', 'element-plus/lib/locale/lang/en', 'element-plus/lib/locale/lang/zh-tw'],
		},
		/*server: {
			host: '0.0.0.0',
			port: env.VITE_PORT as unknown as number,
			open: env.VITE_OPEN,
			proxy: {
				'/gitee': {
					target: 'https://gitee.com',
					ws: true,
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/gitee/, ''),
				},
			},
		},*/
		server: {
			host: '0.0.0.0',
			port: 3001,
			proxy: {
				'/api': {
					// target: 'http://120.24.28.68:39009/api/',
					// target: 'http://183.239.186.195:39009/api/',
					target: 'http://202.104.182.126:39009/api/',
					changeOrigin: true,
					rewrite: (path) => {
						return path.replace(/^\/api/, '');
					},
				},
				'/amap': {
					target: 'https://restapi.amap.com/v3/',
					changeOrigin: true,
					ws: true,
					pathRewrite: {
						'^/amap': '',
					},
				},
			},
		},
		build: {
			outDir: 'dist',
			sourcemap: false,
			chunkSizeWarningLimit: 1500,
			rollupOptions: {
				output: {
					entryFileNames: `assets/[name].[hash].js`,
					chunkFileNames: `assets/[name].[hash].js`,
					assetFileNames: `assets/[name].[hash].[ext]`,
					compact: true,
					manualChunks: {
						vue: ['vue', 'vue-router', 'pinia'],
						echarts: ['echarts'],
					},
				},
			},
		},
		css: { preprocessorOptions: { css: { charset: false } } },
		define: {
			__VUE_I18N_LEGACY_API__: JSON.stringify(false),
			__VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
			__INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
		},
	};
});

export default viteConfig;
